﻿(function (amplify, $, undefined) {
    var properties = ["types", "resources", "define", "decoders"];

    amplify.request_original = amplify.request;

    amplify.request = function (resourceId, data) {
        return $.Deferred(function (def) {
            amplify.request_original({
                resourceId: resourceId,
                data: data,
                success: def.resolve,
                error: def.reject
            });
        }).promise();
    };

    $.each(properties, function (index, key) {
        amplify.request[key] = amplify.request_original[key];
    });

    // replace define impl with one that doesn't overwrite exisintdefinitions
    amplify.request.define = function (resourceId, type, settings) {
        if (typeof type === "string") {
            if (!(type in amplify.request.types)) {
                throw "amplify.request.define: unknown type: " + type;
            }

            settings.resourceId = resourceId;

            // check to make sure it's not already defined
            if (amplify.request.resources[resourceId] === undefined)
                amplify.request.resources[resourceId] =
                    amplify.request.types[type](settings);
        } else {
            // no pre-processor or settings for one-off types (don't invoke)
            amplify.request.resources[resourceId] = type;
        }
    };
})(amplify, jQuery);